<template>
  <div class="dynamic-page-container">
    <div class="page-header">
      <div class="header-content">
        <h1 class="page-title">{{ pageInfo.pageName || "动态页面" }}</h1>
        <p class="page-description" v-if="pageInfo.description">
          {{ pageInfo.description }}
        </p>
        <div class="page-meta">
          <span class="meta-item">
            <i class="el-icon-user"></i>
            创建人: {{ pageInfo.creator || "系统" }}
          </span>
          <span class="meta-item">
            <i class="el-icon-time"></i>
            创建时间: {{ formatTime(pageInfo.createTime) }}
          </span>
        </div>
      </div>
      <div class="header-actions">
        <el-button type="primary" @click="goBack">
          <i class="el-icon-arrow-left"></i>
          返回页面管理
        </el-button>
      </div>
    </div>

    <div class="page-content">
      <div v-if="loading" class="loading-container">
        <i class="el-icon-loading"></i>
        <p>正在加载页面内容...</p>
      </div>

      <div v-else-if="error" class="error-container">
        <i class="el-icon-warning"></i>
        <p>{{ error }}</p>
        <el-button type="primary" @click="goBack">返回页面管理</el-button>
      </div>

      <div v-else class="form-container">
        <v-form-render
          ref="vFormRender"
          :form-json="formJson"
          :form-data="formData"
          :option-data="optionData"
        />
      </div>
    </div>

    <!-- 提交按钮 -->
    <div class="submit-section" v-if="!loading && !error && formJson">
      <el-button type="success" size="large" @click="submitForm">
        <i class="el-icon-check"></i>
        提交表单
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "DynamicPageView",
  data() {
    return {
      loading: true,
      error: null,
      pageInfo: {},
      formJson: null,
      formData: {},
      optionData: {},
    };
  },
  created() {
    this.loadPageData();
  },
  methods: {
    // 加载页面数据
    loadPageData() {
      try {
        // 从URL参数获取页面ID
        const pageId = this.$route.params.pageId;

        if (!pageId) {
          this.error = "未找到页面ID参数";
          this.loading = false;
          return;
        }

        // 从localStorage获取页面列表
        const pagesJson = localStorage.getItem("vform-generated-pages");
        if (!pagesJson) {
          this.error = "未找到页面数据";
          this.loading = false;
          return;
        }

        const pages = JSON.parse(pagesJson);
        const page = pages.find((p) => p.id === pageId);

        if (!page) {
          this.error = `未找到ID为${pageId}的页面`;
          this.loading = false;
          return;
        }

        // 设置页面信息
        this.pageInfo = {
          pageName: page.pageName,
          description: page.description,
          creator: page.creator,
          createTime: page.createTime,
        };

        // 设置表单JSON
        this.formJson = page.formJson;

        if (!this.formJson || !this.formJson.widgetList) {
          this.error = "页面数据格式错误";
          this.loading = false;
          return;
        }

        this.loading = false;
      } catch (error) {
        console.error("加载页面数据失败:", error);
        this.error = "加载页面数据失败：" + error.message;
        this.loading = false;
      }
    },

    // 格式化时间
    formatTime(timeString) {
      if (!timeString) return "未知时间";
      try {
        const date = new Date(timeString);
        return date.toLocaleString("zh-CN", {
          year: "numeric",
          month: "2-digit",
          day: "2-digit",
          hour: "2-digit",
          minute: "2-digit",
        });
      } catch (error) {
        return timeString;
      }
    },

    // 提交表单
    submitForm() {
      this.$refs.vFormRender
        .getFormData()
        .then((formData) => {
          console.log("表单数据:", formData);

          // 显示表单数据
          this.$alert(
            `<pre style="max-height: 400px; overflow: auto; text-align: left">${JSON.stringify(
              formData,
              null,
              2
            )}</pre>`,
            "表单提交成功",
            {
              dangerouslyUseHTMLString: true,
              confirmButtonText: "确定",
            }
          );
        })
        .catch((error) => {
          console.error("表单验证失败:", error);
          this.$message.error("表单验证失败，请检查必填项");
        });
    },

    // 返回页面管理
    goBack() {
      this.$router.push("/pages");
    },
  },
};
</script>

<style scoped>
.dynamic-page-container {
  min-height: 100vh;
  background: #f5f7fa;
  display: flex;
  flex-direction: column;
}

.page-header {
  background: white;
  padding: 20px 30px;
  border-bottom: 1px solid #e4e7ed;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  box-shadow: 0 2px 4px rgba(0, 21, 41, 0.08);
}

.header-content {
  flex: 1;
}

.page-title {
  font-size: 28px;
  font-weight: 600;
  color: #303133;
  margin: 0 0 10px 0;
}

.page-description {
  font-size: 16px;
  color: #606266;
  margin: 0 0 15px 0;
  line-height: 1.5;
}

.page-meta {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #909399;
}

.meta-item i {
  margin-right: 5px;
  color: #409eff;
}

.header-actions {
  margin-left: 20px;
}

.page-content {
  flex: 1;
  padding: 30px;
}

.loading-container,
.error-container {
  text-align: center;
  padding: 60px 20px;
}

.loading-container i {
  font-size: 48px;
  color: #409eff;
  margin-bottom: 20px;
}

.error-container i {
  font-size: 48px;
  color: #f56c6c;
  margin-bottom: 20px;
}

.loading-container p,
.error-container p {
  font-size: 18px;
  color: #606266;
  margin-bottom: 20px;
}

.form-container {
  background: white;
  border-radius: 8px;
  padding: 30px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  min-height: 400px;
}

.submit-section {
  background: white;
  padding: 20px 30px;
  border-top: 1px solid #e4e7ed;
  text-align: center;
  box-shadow: 0 -2px 4px rgba(0, 21, 41, 0.08);
}

.submit-section .el-button {
  padding: 12px 30px;
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    padding: 15px 20px;
    flex-direction: column;
    align-items: stretch;
  }

  .header-actions {
    margin-left: 0;
    margin-top: 15px;
    text-align: center;
  }

  .page-title {
    font-size: 24px;
  }

  .page-description {
    font-size: 14px;
  }

  .page-meta {
    gap: 15px;
  }

  .meta-item {
    font-size: 13px;
  }

  .page-content {
    padding: 20px 15px;
  }

  .form-container {
    padding: 20px 15px;
  }

  .submit-section {
    padding: 15px 20px;
  }
}

/* 表单样式优化 */
.form-container >>> .el-form {
  max-width: 800px;
  margin: 0 auto;
}

.form-container >>> .el-form-item__label {
  font-weight: 500;
  color: #303133;
}

.form-container >>> .el-input__inner,
.form-container >>> .el-textarea__inner {
  border-radius: 4px;
}

.form-container >>> .el-button {
  border-radius: 4px;
}
</style>
